package com.xiaoyu.izf.service;

import com.google.common.collect.Lists;
import com.xiaoyu.izf.model.GoodsOrder;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.VipOrder;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.repository.GoodsOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Service
public class GoodsOrderService extends BaseService {

    @Autowired
    private GoodsOrderDao goodsOrderDao;

    public GoodsOrder findOne(String id){
        return goodsOrderDao.findOneById(id);
    }

    public GoodsOrder save(GoodsOrder order){
        return goodsOrderDao.save(order);
    }

    public DataTableBean list(ParamListBean param,UserInfo user){
        PageRequest pageRequest = buildPageRequestSort(param.getPageNo(), param.getPageLength(), "id",param.getFieldName(),param.getCompositorType());

        //添加查询条件
        Page<GoodsOrder> page = goodsOrderDao.findAll(new Specification<GoodsOrder>() {

            /**
             * 构建查询条件（Predicate断言对象）
             * @param root entity：UserInfo
             * @param query
             * @param cb 条件构建器
             * @return
             */
            @Override
            public Predicate toPredicate(Root<GoodsOrder> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //断言列表，可以添加多个断言
                List<Predicate> predicates = Lists.newArrayList();
                //准备结合断言链
                predicates.add(cb.conjunction());
                if (StringUtils.hasLength(param.getParam().get("orderStatus"))){
                    predicates.add(
                            cb.equal(root.get("orderStatus").as(Integer.class),param.getParam().get("orderStatus")));
                }

                if (StringUtils.hasLength(param.getParam().get("orderId"))){
                    predicates.add(
                            cb.equal(root.get("id").as(String.class),param.getParam().get("orderId")));
                }

                if (StringUtils.hasLength(param.getParam().get("goodsTitle"))){
                    predicates.add(
                            cb.like(root.get("goodsInfo").get("title").as(String.class),"%"+param.getParam().get("goodsTitle")+"%"));
                }

                if (StringUtils.hasLength(param.getParam().get("userName"))){
                    predicates.add(
                            cb.equal(root.get("buyUser").get("userName").as(String.class),param.getParam().get("userName")));
                }
                predicates.add(cb.equal(root.get("isTest").as(Integer.class),0));
                if (user.getIsAdmin()==0){
                    predicates.add(
                            cb.equal(root.get("uid").as(Integer.class),user.getId()));
                }
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        },pageRequest);
//		//无查询条件
//		Page<UserInfo> page = userInfoDao.findAll(pageRequest);

        return dataTableBean(page,param);
    }
}
